#ifndef SENSORTRAYCONVEYER_H
#define SENSORTRAYCONVEYER_H

#include "AxisModule/singleaxis.h"
#include "CompositeElement/sccylinder.h"
#include "errorHandling/kicktrayerror.h"
#include "namedefinition.h"
#include "sensortrayconveyerconfig.h"
#include "systemCore/worker.h"
#include "workers/SensorTrayElevator/sensortrayoutputelevator.h"

class BonderWorkHolder;

class SensorTrayConveyer : public Worker
{
    Q_OBJECT
public:
    explicit SensorTrayConveyer(SensorTrayOutputElevator *stoe, QObject *parent = nullptr);

    SensorTrayConveyerConfig *getStcConfig() const;
    bool beltZCanMove() const;

    // Worker interface
protected:
    virtual void initEventHandle() override;
    virtual void startEventHandle(const WorkerEvent &event) override;
    virtual void stopEventHandle() override;
    virtual void allWorkerStopped() override {}
    virtual void purgeEventHandle() override;
    virtual void resetEventHandle() override {}
    virtual void homeWorker(bool moveToReadyPosAfterHome) override;

public slots:
    void pushOutTray();
    void makeSureHasNotTrayAtTheExit(bool moveBeltToCheck);

private:
    bool detectHasTrayAtTheExit(bool moveBeltToCheck);
    bool isAnySensorGotSignal();
    bool trayArrivedExit();

private:
    friend class BonderWorkHolder;

    bool m_beltZCanMove = true;

    SensorTrayOutputElevator *stoe = nullptr;
    SensorTrayConveyerConfig *stcConfig;
    ConfigFile *stcConfigFile;

    SCCylinder *stcKicker;
    SCDI *stcRightTraySensor;
    SCDI *stcLeftTraySensor;
    SCAxis *stcBeltZ;
    SCAxis *stcBelt;

    SCAxis::Direction beltRunDir;
};

#endif    // SENSORTRAYCONVEYER_H
